home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / System / Sample 2.4 Think C distribution / gff.c < prev    next >
Text File  |  1990-07-11  |  5KB  |  173 lines

  1. /*______________________________________________________________________
  2.  
  3.     gff.c - Get File or Folder.
  4.     
  5.     Copyright © 1988, 1989, 1990 Northwestern University.  Permission is granted
  6.     to use this code in your own projects, provided you give credit to both
  7.     John Norstad and Northwestern University in your about box or document.
  8.     
  9.     This reusable module presents a modified standard open file dialog.
  10.     There are two extra buttons which can be used to pick a folder instead
  11.     of a file.  
  12.     
  13.     The caller supplies the id of the dialog to be used.  It must be a 
  14.     standard open file dialog with two additional buttons:
  15.     
  16.     item 11 = Select current folder button.
  17.     item 12 = Folder button.
  18. _____________________________________________________________________*/
  19.  
  20.  
  21. #include "gff.h"
  22.  
  23. #define nil 0
  24.  
  25. /*______________________________________________________________________
  26.  
  27.     Constant Definitions.
  28. _____________________________________________________________________*/
  29.  
  30.  
  31. #define getSCFold        11            /* item number of select current folder 
  32.                                             button */
  33. #define getFold        12            /* item number of folder button. */
  34.  
  35.  
  36.  
  37.  
  38. /*______________________________________________________________________
  39.  
  40.     Global Variables.
  41. _____________________________________________________________________*/
  42.  
  43.  
  44. static SFReply        *Reply;                /* pointer to reply record */
  45. static Boolean        FolderActive;        /* true if folder button is active */
  46. static short        Item;                    /* item number of selected item */
  47. static OSType        DirID;                /* dir id of selected folder */
  48.  
  49. /*______________________________________________________________________
  50.  
  51.     HiliteFolderButton - Hilite the Folder Button.
  52.     
  53.     Entry:    theDialog = pointer to dialog.
  54.                 active = true to make button active, false to make it inacative.
  55. _____________________________________________________________________*/
  56.  
  57.  
  58. static void HiliteFolderButton (DialogPtr theDialog, Boolean active)
  59.  
  60. {
  61.     short                itemType;            /* item type */
  62.     Handle            item;                    /* handle to item */
  63.     Rect                box;                    /* item rectangle */
  64.  
  65.     GetDItem(theDialog, getFold, &itemType, &item, &box);
  66.     HiliteControl((ControlHandle)item, active ? 0 : 255);
  67.     FolderActive = active;
  68. }
  69.  
  70. /*______________________________________________________________________
  71.  
  72.     MyHook - SFPGetFile Dialog Hook.
  73.     
  74.     Entry:    item = item number of selected item.
  75.                 theDialog = pointer to dialog.
  76.                 
  77.     Exit:        function result = item number of selected item.
  78. _____________________________________________________________________*/
  79.  
  80.  
  81. static pascal short MyHook (short item, DialogPtr theDialog)
  82.  
  83. {
  84.     Item = item;
  85.  
  86.     /* Hilite the Folder button iff a folder is currently selected. */
  87.  
  88.     if (!*(Reply->fName) && Reply->fType) {
  89.         if (!FolderActive) HiliteFolderButton(theDialog, true);
  90.     } else {
  91.         if (FolderActive) HiliteFolderButton(theDialog, false);
  92.     };
  93.     
  94.     /* Check for our two additional buttons.  If either one was selected
  95.         pretend the Cancel button was selected to force SFPGetFile to return. */
  96.     
  97.     if (item == getSCFold) return getCancel;
  98.     if (item == getFold) {
  99.         DirID = Reply->fType;
  100.         return getCancel;
  101.     };
  102.     
  103.     /* For all other buttons do nothing. */
  104.     
  105.     return item;
  106. }
  107.  
  108. /*______________________________________________________________________
  109.  
  110.     gff_get - Get File or Folder
  111.     
  112.     Entry:    where = pointer to location of top left corner of dialog, in 
  113.                     global coords.
  114.                 prompt = prompt string.
  115.                 fileFilter = file selection filter procedure.
  116.                 numTypes = number of file types.
  117.                 typeList = array of file types.
  118.                 reply = pointer to standard file package reply record.
  119.                 dlgID = dialog resource id.
  120.                 
  121.     Exit:        reply->good = true if something selected.
  122.                 
  123.                 if a file was selected:
  124.                 
  125.                 reply->vRefNum = vol ref num or working directory ref num of 
  126.                     volume or directory that contains the file.
  127.                 reply->version = vol ref num of volume that contains the file.
  128.                 reply->fName = name of the file.
  129.                 
  130.                 if a folder was selected:
  131.                 
  132.                 reply->vRefNum = vRefNum of volume containing folder.
  133.                 reply->fType = directory id of folder.
  134.                 reply->fName = empty string.
  135. _____________________________________________________________________*/
  136.         
  137.         
  138. void gff_Get (Point *where, Str255 *prompt, FileFilterProcPtr fileFilter, 
  139.     short numTypes, SFTypeList *typeList, SFReply *reply, 
  140.     short dlgID)
  141.  
  142. {
  143.     /* Initialize global variables. */
  144.  
  145.     Reply = reply;
  146.     FolderActive = true;
  147.     
  148.     /* Call the standard file package. */
  149.  
  150.     SFPGetFile(*where, (StringPtr)prompt, (FilterProc)fileFilter, numTypes, typeList, (ProcPtr)MyHook,
  151.         reply, dlgID, (FilterProc)nil);
  152.     
  153.     /* Return if dialog canceled. */
  154.     
  155.     if (Item == getCancel) return;
  156.     
  157.     /* If file selected set version field to vol ref num and return. */
  158.     
  159.     if (reply->good) {
  160.         reply->version = - SFSaveDisk;
  161.         return;
  162.     };
  163.     
  164.     /* If a folder was selected clear the file name and set the vRefNum
  165.         and directory id. */
  166.     
  167.     reply->good = true;
  168.     *(reply->fName) = 0;
  169.     reply->vRefNum = - SFSaveDisk;
  170.     reply->fType = (Item == getFold) ? DirID : CurDirStore;
  171.     return;
  172. }
  173.